package com.example.shushiyunsst.fragment.main.transaction.child.kline_chart.k_line_chart_portrait_content

import android.annotation.SuppressLint
import android.graphics.drawable.ClipDrawable
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.fastjson.JSON
import com.example.library_common.adapter.JssBaseQuickAdapter
import com.example.library_common.adapter.JssBaseViewHolder
import com.example.library_common.base.LazyFragment
import com.example.library_common.networking.RestClient
import com.example.library_common.utils.even.MessageWrap
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.main.transaction.child.TRANSFER_INDEX_DATA_REFRESH
import com.example.shushiyunsst.fragment.main.transaction.child.kline_chart.KlineChartInterface
import com.example.shushiyunsst.fragment.main.transaction.child.kline_chart.bean.KlineChartDeepBean
import com.example.wallet.view.depth_map_view.DepthDataBean
import com.example.wallet.view.depth_map_view.DepthMapView


//深度
class KlineChartPortraitContentDeepFragment : LazyFragment(), KlineChartPortraitLabel {

    companion object {
        fun newInstance(): KlineChartPortraitContentDeepFragment {
            val args = Bundle()
            val fragment = KlineChartPortraitContentDeepFragment()
            fragment.arguments = args
            return fragment
        }

        const val MAX_LEVEL_CLIP_DRAWABLE = 10000
    }

    private lateinit var numLeftTv: TextView

    private lateinit var priceTv: TextView

    private lateinit var numRightTv: TextView

    private lateinit var mSellLayout: RecyclerView

    private lateinit var mBuyLayout: RecyclerView

    private lateinit var depth_view: DepthMapView

    private var transactionUIInvokeListener: KlineChartInterface? = null

    private var mSellAdapter: JssBaseQuickAdapter<KlineChartDeepBean.ListBean>? = null

    private var mBuyAdapter: JssBaseQuickAdapter<KlineChartDeepBean.ListBean>? = null

    private var onRefreshing = false

    override fun setTransactionUIInvokeListener(listener: KlineChartInterface?) {
        this.transactionUIInvokeListener = listener

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mSuperBackPressedSupport = false
    }

    override fun getTitle(): String {
        return "深度"
    }


    override fun lazyInit() {
        netRequest()
    }

    @SuppressLint("SetTextI18n")
    private fun netRequest() {
        if (onRefreshing && isSupportVisible) return
        onRefreshing = true
        val transIndexNode = transactionUIInvokeListener?.getTransIndexNode()
        val tokenName = transIndexNode?.iconType?.tokenName ?: ""
        val mTransferIcon = transIndexNode?.iconType?.getToTransferIcon()
        numLeftTv.text = "数量($tokenName)"
        priceTv.text = "价格($mTransferIcon)"
        numRightTv.text = "数量($tokenName)"

        val map = mapOf(
            "tokenType" to tokenName
        )
        RestClient.builder()
            .raw(JSON.toJSONString(map))
            .url(R.string.queryTxDetail)
            .success {
                parseQueryTxDetail(it)
                onRefreshing = false
            }.error { _, _ ->
                onRefreshing = false
            }.failure {
                onRefreshing = false
            }
            .build()
            .post()
    }

    override fun handMessage(msg: MessageWrap?) {
        super.handMessage(msg)
        when (msg?.message) {
            TRANSFER_INDEX_DATA_REFRESH -> {
                lazyInit()
            }
        }
    }

    private fun parseQueryTxDetail(it: String?) {
        val obj = JSON.parseObject(it, KlineChartDeepBean::class.java)
        val buyList = obj.buyList
        mBuyAdapter?.clears()
        mBuyAdapter?.setList(buyList)

        val sellList = obj.sellList
        mSellAdapter?.clears()
        mSellAdapter?.setList(sellList)

        val listDepthBuy = mutableListOf<DepthDataBean>()
        val listDepthSell = mutableListOf<DepthDataBean>()

        for (e in buyList) {
            val mDepthDataBean = DepthDataBean()
            mDepthDataBean.price = e.txPrice.toFloat()
            mDepthDataBean.volume = e.txNums * 1.0f
            listDepthBuy.add(mDepthDataBean)
        }
        for (e in sellList) {
            val mDepthDataBean = DepthDataBean()
            mDepthDataBean.price = e.txPrice.toFloat()
            mDepthDataBean.volume = e.txNums * 1.0f
            listDepthSell.add(mDepthDataBean)
        }

        depth_view.setData(listDepthBuy, listDepthSell)
    }

    override fun initView(view: View) {
        numLeftTv = view.findViewById(R.id.numLeftTv)
        priceTv = view.findViewById(R.id.priceTv)
        numRightTv = view.findViewById(R.id.numRightTv)
        mSellLayout = view.findViewById(R.id.mSellLayout)
        mBuyLayout = view.findViewById(R.id.mBuyLayout)
        depth_view = view.findViewById(R.id.depth_view)

        mSellLayout.setHasFixedSize(true)
        mSellLayout.layoutManager =
            LinearLayoutManager(_mActivity, LinearLayoutManager.VERTICAL, false)

        mBuyLayout.setHasFixedSize(true)
        mBuyLayout.layoutManager =
            LinearLayoutManager(_mActivity, LinearLayoutManager.VERTICAL, false)

        mSellAdapter = object : JssBaseQuickAdapter<KlineChartDeepBean.ListBean>(
            R.layout.k_line_chart_portrait_content_sell_deep_fragment
        ) {
            override fun convert(holder: JssBaseViewHolder?, item: KlineChartDeepBean.ListBean?) {
                super.convert(holder, item)
                holder?.setText(R.id.numTv, "${item?.txNums}")
                    ?.setText(R.id.priceTv, "${item?.txPrice}")
                val mProgressView = holder?.getView<View>(R.id.mProgressView)
                val background = mProgressView?.background
                if (background is ClipDrawable) {
                    background.level = (MAX_LEVEL_CLIP_DRAWABLE * (item?.bili ?: 0.0f)).toInt()
                }
            }
        }

        mBuyAdapter = object : JssBaseQuickAdapter<KlineChartDeepBean.ListBean>(
            R.layout.k_line_chart_portrait_content_buy_deep_fragment
        ) {
            override fun convert(holder: JssBaseViewHolder?, item: KlineChartDeepBean.ListBean?) {
                super.convert(holder, item)
                holder?.setText(R.id.numTv, "${item?.txNums}")
                    ?.setText(R.id.priceTv, "${item?.txPrice}")
                val mProgressView = holder?.getView<View>(R.id.mProgressView)
                val background = mProgressView?.background
                if (background is ClipDrawable) {

                    background.level = (MAX_LEVEL_CLIP_DRAWABLE * (item?.bili ?: 0.0f)).toInt()
                }
            }
        }
        mSellLayout.adapter = mSellAdapter
        mBuyLayout.adapter = mBuyAdapter
    }

    override fun getLayoutId(): Any {
        return R.layout.k_line_chart_portrait_content_deep_fragment
    }


}